package dao

import "time"

type l2net struct {
	L2_id int `json:"l2_id" gorm:"column:l2_id"`
	Uid int `json:"uid"`
	Sid int `json:"sid"`
	Uuid string `json:"uuid" gorm:"column:uuid"`
	ZoneUuid string `json:"zone_uuid" gorm:"column:zoneUuid"`
	Name string `json:"name" gorm:"column:name"`
	PhysicalInterface string `json:"physicalInterface" gorm:"column:physicalInterface"`
	Type string `json:"type" gorm:"column:type"`
	Ctime int64 `json:"c_time" gorm:"column:c_time"`
	Dtime int64 `json:"d_time" gorm:"column:d_time"`
}

type l2netUuid struct {
	Uuid string `json:"uuid" gorm:"column:uuid"`
}
func (t *l2net)Create(d map[string]interface{}) (bool,int){

	var l2 l2net
	l2.Uuid = d["uuid"].(string)
	l2.ZoneUuid = d["zoneUuid"].(string)
	l2.Name = d["name"].(string)
	l2.PhysicalInterface = d["physicalInterface"].(string)
	l2.Type = d["type"].(string)
	l2.Ctime = time.Now().Unix()
	l2.Uid = d["uid"].(int)
	l2.Sid = d["sid"].(int)

	err := db.Create(&l2).Error
	b := true
	if err != nil{
		b = false
	}
	var id []int
	db.Raw("select LAST_INSERT_ID() as id").Pluck("id", &id)
	return b,id[0]
}
func (t *l2net) Delete(w map[string]interface{}) bool {

	err := db.Where(w).Delete(&t).Error
	b := true
	if err !=nil{
		b = false
	}
	return b
}

func  (t *l2net)UpdateByUuid(uuid string, data map[string]interface{}) bool {
	err := db.Model(&t).Where("uuid=?",  uuid).Updates(data).Error
	if err != nil {
		return false
	}
	return true
}

func (t *l2net)FindBySid(sid int) ([]*l2netUuid,error) {
	var l2netUuid []*l2netUuid
	err := db.Model(&l2netUuid).Where("sid=?",  sid).Find(&l2netUuid).Error
	if err != nil {
		return nil, err
	}
	return l2netUuid,nil
}

var L2Net = &l2net{}
